package com.klangner.balls
{
  import flash.display.Sprite;
  import flash.geom.Point;

  public class Ball extends Sprite
  {
    private var _radius:Number = 30;
    private var _color:uint = 0x0000ff;
    private var _velocity:Point = new Point();
    private var _physics:CollisionDetector;

    public function Ball(startX:Number, startY:Number, cd:CollisionDetector){
      x = startX;
      y = startY;
      _physics = cd;
      updateGraphics();
    }
    
    public function get mass():Number{
      return _radius;
    }

    public function get radius():Number{
      return _radius;
    }

    public function set radius(r:Number):void{
      _radius = r;
    }

    public function get velocity():Point{
      return _velocity;
    }

    public function move():void{
      var oldPos:Point = new Point(x, y);
      
      x += _velocity.x;
      y += _velocity.y;
      
      if(!_physics.checkCollision(this)){
        x = oldPos.x;
        y = oldPos.y;
      }
    }
    
    public function setColor(red:int, green:int, blue:int):void{
      
      _color = red << 16 | green << 8 | blue;
      updateGraphics(); 
    }

    public function setVelocity(x:Number, y:Number):void{
      _velocity.x = x;
      _velocity.y = y;
    }

    private function updateGraphics():void{
      graphics.clear();
      graphics.beginFill(_color);
      graphics.drawCircle(0, 0, _radius);
      graphics.endFill();
      
    }
  }
}